<?php

namespace App\Admin\Controllers\Goods;

use App\Models\Goods\Goods;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\MessageBag;

class GoodsController extends Controller
{
    use ModelForm;
    protected $header = '商品列表';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Goods::class, function (Grid $grid) {
            $grid->model()->orderBy('id','DESC');
            $grid->id('编号')->sortable();
            $grid->goods_icon('图片')->image('','50','50');
            $grid->goods_name('商品名称');
            $grid->money('价格');
            $grid->column('标签')->switchGroup([
                'status' => '上架',
                'goods_recommend' => '推荐',
            ], [
                'on' => ['text' => '是'],
                'off' => ['text' => '否'],
            ]);
            $grid->to_examine('是否审核')->using(['1' => '已审核', '2' => '未审核']);
            $grid->create_at('创建时间');

            //搜索栏
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('goods_name','商品名称');
            });
            //操作列
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Goods::class, function (Form $form) {

            $form->tools(function (Form\Tools $tools) {
                // 去掉`查看`按钮
                $tools->disableView();
            });

            $form->footer(function ($footer) {
                // 去掉`查看`checkbox
                $footer->disableViewCheck();
                // 去掉`继续编辑`checkbox
                $footer->disableEditingCheck();
            });

            $form->display('id', '编号');
            $form->text('goods_name', '商品名称')->rules('required');
            $form->image('goods_icon','图片');
            $form->currency('money', '价格')->symbol('￥');
            $form->currency('goods_value', '商品价值')->symbol('￥');
            $form->switch('status', '是否上架')->states([
                'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
                'off' => ['value' => 2, 'text' => '否', 'color' => 'danger'],
            ]);
            $form->switch('goods_recommend', '是否推荐')->states([
                'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
            ]);
            $form->textarea('describe', '描述');
            $form->textarea('exchange_rules', '兑换规则');
            $form->textarea('after_sale_treatment', '售后处理');
            $form->display('create_at', '创建时间');
            /*$form->saving(function ($form) {

                $success = new MessageBag([
                    'title'   => 'title...',
                    'message' => 'message....',
                ]);

                return back()->with(compact('success'));
            });*/

        });
    }


}
